#include "usart.h"
DATA datatest;
uint8_t Buf[300];
uint8_t isAction = 1;
uint8_t isBlack;
uint8_t isWhite;
uint8_t isPlayerDown = 1;
uint8_t capCam;
void Data_Analysis(uint8_t cmd, uint8_t *data_ptr, uint8_t len) // 鏁版嵁瑙ｆ瀽
{
    for (uint8_t i; i < len; i++)
    {
        datatest.data8[i] = *(data_ptr + i);
    }
    if (cmd == 0)
    {
        if (datatest.dataf == 0)
        {
            isAction = 0;
        }
        else if (datatest.dataf == 1)
        {
            isAction = 1;
            isPlayerDown = 0;
        }
    }
    else if (cmd == 3)
    {
    }
    else if (cmd == 4)
    {
        isBlack = 1;
        isWhite = 0;
        isAction = 0;
        capCam = 0;
        if (datatest.dataf == 1)
        {
            isPlayerDown = 1;
            capCam = 1;
        }
        // std::cout << "cmd:" << cmd << std::endl;
        printf("cmd:%d\r\n", cmd);
    }
    else if (cmd == 5)
    {
        isBlack = 0;
        isWhite = 1;
        isAction = 0;
        capCam = 0;
        isPlayerDown = 0;
        if (datatest.dataf == 1)
        {
            isPlayerDown = 1;
            capCam = 1;
        }
    }
    // // vofaTransmit(0, data.dataf);
}
void Receive(uint8_t bytedata)
{
    static uint8_t step = 0; // 鐘舵?佸彉閲忓垵濮嬪寲涓?0 鍦ㄥ嚱鏁颁腑蹇呴』涓洪潤鎬佸彉閲?
    static uint8_t cnt = 0, len, cmd, *data_ptr;
    static uint16_t crc16;
    // 杩涜鏁版嵁瑙ｆ瀽 鐘舵?佹満
    switch (step)
    {
    case 0: // 鎺ユ敹甯уご1鐘舵??
        if (bytedata == 0xA5)
        {
            step++;
            cnt = 0;
            Buf[cnt++] = bytedata;
        }
        std::cout << 1 << std::endl;
        break;
    case 1: // 鎺ユ敹甯уご2鐘舵??
        if (bytedata == 0x5A)
        {
            step++;
            Buf[cnt++] = bytedata;
        }
        else if (bytedata == 0xA5)
        {
            step = 1;
        }
        else
        {
            step = 0;
        }
        std::cout << 2 << std::endl;
        break;
    case 2: // 鎺ユ敹鏁版嵁闀垮害瀛楄妭鐘舵??
        step++;
        Buf[cnt++] = bytedata;
        len = bytedata;
        std::cout << 3 << std::endl;
        break;
    case 3: // 鎺ユ敹鍛戒护瀛楄妭鐘舵??
        step++;
        Buf[cnt++] = bytedata;
        cmd = bytedata;
        data_ptr = &Buf[cnt]; // 璁板綍鏁版嵁鎸囬拡棣栧湴鍧?
        if (len == 0)
            step++; // 鏁版嵁瀛楄妭闀垮害涓?0鍒欒烦杩囨暟鎹帴鏀剁姸鎬?
        std::cout << 4 << std::endl;
        break;
    case 4: // 鎺ユ敹len瀛楄妭鏁版嵁鐘舵??
        Buf[cnt++] = bytedata;
        if (data_ptr + len == &Buf[cnt]) // 鍒╃敤鎸囬拡鍦板潃鍋忕Щ鍒ゆ柇鏄惁鎺ユ敹瀹宭en浣嶆暟鎹?
        {
            step++;
        }
        std::cout << 5 << std::endl;
        break;
    case 5:                   // 鎺ユ敹甯у熬
        if (bytedata == 0x7E) // 甯у熬鎺ユ敹姝ｇ‘
        {
            Data_Analysis(cmd, data_ptr, len); // 鏁版嵁瑙ｆ瀽
            step = 0;
        }
        else if (bytedata == 0xA5)
        {
            step = 1;
        }
        else
        {
            step = 0;
        }
        std::cout << 6 << std::endl;
        break;
    default:
        step = 0;
        break; // 澶氫綑鐘舵?侊紝姝ｅ父鎯呭喌涓嬩笉鍙兘鍑虹幇
    }
}